package com.spic.business.accident.feedback.service.impl;

import com.spic.business.accident.feedback.domain.AccidentExperecnceBackAction;
import com.spic.business.accident.feedback.domain.AccidentExperenceBack;
import com.spic.business.accident.feedback.mapper.AccidentExperenceBackMapper;
import com.spic.business.accident.feedback.service.IAccidentExperenceBackService;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 经验反馈单Service业务层处理
 *
 * @author spic
 * @date 2021-12-20
 */
@Service
public class AccidentExperenceBackServiceImpl implements IAccidentExperenceBackService {
    @Autowired
    private AccidentExperenceBackMapper accidentExperenceBackMapper;

    /**
     * 查询经验反馈单
     *
     * @param id 经验反馈单主键
     * @return 经验反馈单
     */
    @Override
    public AccidentExperenceBack selectAccidentExperenceBackById(Long id) {
        return accidentExperenceBackMapper.selectAccidentExperenceBackById(id);
    }

    /**
     * 查询经验反馈单列表
     *
     * @param accidentExperenceBack 经验反馈单
     * @return 经验反馈单
     */
    @Override
    @BusinessDataScope(menuPath = "business/accident/feedback/index")
    public List<AccidentExperenceBack> selectAccidentExperenceBackList(AccidentExperenceBack accidentExperenceBack) {
        return accidentExperenceBackMapper.selectAccidentExperenceBackList(accidentExperenceBack);
    }

    /**
     * 新增经验反馈单
     *
     * @param accidentExperenceBack 经验反馈单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAccidentExperenceBack(AccidentExperenceBack accidentExperenceBack) {
        accidentExperenceBack.setCreateTime(DateUtils.getNowDate());
        int rows = accidentExperenceBackMapper.insertAccidentExperenceBack(accidentExperenceBack);
        insertAccidentExperecnceBackAction(accidentExperenceBack);
        return rows;
    }

    /**
     * 修改经验反馈单
     *
     * @param accidentExperenceBack 经验反馈单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAccidentExperenceBack(AccidentExperenceBack accidentExperenceBack) {
        accidentExperenceBackMapper.deleteAccidentExperecnceBackActionByAccidentExperenceBackId(accidentExperenceBack.getId());
        insertAccidentExperecnceBackAction(accidentExperenceBack);
        return accidentExperenceBackMapper.updateAccidentExperenceBack(accidentExperenceBack);
    }

    /**
     * 批量删除经验反馈单
     *
     * @param ids 需要删除的经验反馈单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAccidentExperenceBackByIds(Long[] ids) {
        accidentExperenceBackMapper.deleteAccidentExperecnceBackActionByIds(ids);
        return accidentExperenceBackMapper.deleteAccidentExperenceBackByIds(ids);
    }

    /**
     * 删除经验反馈单信息
     *
     * @param id 经验反馈单主键
     * @return 结果
     */
    @Override
    public int deleteAccidentExperenceBackById(Long id) {
        accidentExperenceBackMapper.deleteAccidentExperecnceBackActionByAccidentExperenceBackId(id);
        return accidentExperenceBackMapper.deleteAccidentExperenceBackById(id);
    }

    /**
     * 新增经验反馈行动明细信息
     *
     * @param accidentExperenceBack 经验反馈单对象
     */
    public void insertAccidentExperecnceBackAction(AccidentExperenceBack accidentExperenceBack) {
        List<AccidentExperecnceBackAction> accidentExperecnceBackActionList = accidentExperenceBack.getAccidentExperecnceBackActionList();
        Long id = accidentExperenceBack.getId();
        if (StringUtils.isNotNull(accidentExperecnceBackActionList)) {
            List<AccidentExperecnceBackAction> list = new ArrayList<AccidentExperecnceBackAction>();
            for (AccidentExperecnceBackAction accidentExperecnceBackAction : accidentExperecnceBackActionList) {
                accidentExperecnceBackAction.setAccidentExperenceBackId(id);
                list.add(accidentExperecnceBackAction);
            }
            if (list.size() > 0) {
                accidentExperenceBackMapper.batchAccidentExperecnceBackAction(list);
            }
        }
    }
}
